package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/5/6
 * description:
 */
public class FaultSuccessViewModel extends BaseViewModel {
    public String id;

    public FaultModel model;

    public SingleLiveEvent<String[]> confirmStatusEvent=new SingleLiveEvent<>();//修复状态
    public SingleLiveEvent<String> confirmStatusText=new SingleLiveEvent<>();//修复状态
    public int confirmStatus;//1已修复 2未修复

    public SingleLiveEvent<String[]> affectsWorkEvent=new SingleLiveEvent<>();//是否影响工作
    public SingleLiveEvent<String> affectsWorkText=new SingleLiveEvent<>();
    public int affectsWork;//1是 2否

    public SingleLiveEvent<String[]> arriveIntimeEvent=new SingleLiveEvent<>();//是否及时到达现场
    public SingleLiveEvent<String> arriveIntimeText=new SingleLiveEvent<>();
    public int arriveIntime;//1是 2否

    public SingleLiveEvent<String[]> damageOthersEvent=new SingleLiveEvent<>();//是否损坏其他设备
    public SingleLiveEvent<String> damageOthersText=new SingleLiveEvent<>();
    public int damageOthers;//1是 2否

    public SingleLiveEvent<String> remark=new SingleLiveEvent<>();//备注

    public FaultSuccessViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
    }

    @Override
    protected void init() {
        super.init();
    }


    /**
     * 保存点击事件
     */
    public BindingCommand commit=new BindingCommand(() -> {
        repairConfirm();
    });


    /**
     * 维修状态
     */
    public BindingCommand selectConfirm=new BindingCommand(() -> {
        confirmStatusEvent.setValue(new String[]{"已修复", "未修复"});
    });
    /**
     * 设置维修状态
     * @param str
     */
    public void setConfirmStatus(String str){
        confirmStatusText.setValue(str);
        if(str.equals("已修复")){
            confirmStatus=1;
        }else if(str.equals("未修复")){
            confirmStatus=2;
        }
    }


    /**
     * 是否影响工作
     */
    public BindingCommand selectAffectsWork=new BindingCommand(() -> {
        affectsWorkEvent.setValue(new String[]{"是", "否"});
    });
    /**
     * 设置是否影响工作
     * @param str
     */
    public void setAffectsWork(String str){
        affectsWorkText.setValue(str);
        if(str.equals("是")){
            affectsWork=1;
        }else if(str.equals("否")){
            affectsWork=2;
        }
    }


    /**
     * 是否及时到达现场
     */
    public BindingCommand selectArriveIntime=new BindingCommand(() -> {
        arriveIntimeEvent.setValue(new String[]{"是", "否"});
    });
    /**
     * 是否及时到达现场
     * @param str
     */
    public void setArriveIntime(String str){
        arriveIntimeText.setValue(str);
        if(str.equals("是")){
            arriveIntime=1;
        }else if(str.equals("否")){
            arriveIntime=2;
        }
    }


    /**
     * 是否损坏其他设备
     */
    public BindingCommand selectDamageOthers=new BindingCommand(() -> {
        damageOthersEvent.setValue(new String[]{"是", "否"});
    });
    /**
     * 是否及时到达现场
     * @param str
     */
    public void setDamageOthers(String str){
        damageOthersText.setValue(str);
        if(str.equals("是")){
            damageOthers=1;
        }else if(str.equals("否")){
            damageOthers=2;
        }
    }


    /**
     * 修复确认请求
     */
    public void repairConfirm(){
        if(confirmStatus==0){
            ToastUtils.show("请选择修复情况");
            return;
        }
        if(affectsWork==0){
            ToastUtils.show("请选择是否影响工作");
            return;
        }
        if(arriveIntime==0){
            ToastUtils.show("请选择是否及时到达现场");
            return;
        }
        if(damageOthers==0){
            ToastUtils.show("请选择是否损坏其他设备");
            return;
        }

        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",id);
        map.put("confirmStatus",confirmStatus);
        map.put("affectsWork",affectsWork);
        map.put("arriveIntime",arriveIntime);
        map.put("damageOthers",damageOthers);
        if(StringUtils.isNotEmpty(remark.getValue())){
            map.put("remark",remark.getValue());
        }
        showDialog();
        model.repairConfirm(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("保存成功");
                EventBus.getDefault().post(new FaultDetailEvent());
                EventBus.getDefault().post(new FaultListEvent());
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }
}
